package it.maganar.lesscaos.session;

import java.util.List;

import it.maganar.lesscaos.entity.User;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 * Session Bean implementation class RegistrationManagerBean
 */
@SuppressWarnings("unchecked")
@Stateless
public class RegistrationManagerBean implements RegistrationManagerLocal {

	@PersistenceContext(unitName="lessDB")
	private EntityManager manager;
	
    public RegistrationManagerBean() {
        
    }
    
    public boolean inserisciRegistrazione(String nomeUtente, String password, String nome, String cognome, String nTelefono){
    	List utenti = manager.createQuery("select U from User as U where U.nomeUtente = ?1").setParameter(1, nomeUtente).getResultList();
    	if(!utenti.isEmpty()){
    		return false;
    	}
    	else {
    		User u = new User();
    		u.setNome(nome);
    		u.setCognome(cognome);
    		u.setNomeUtente(nomeUtente);
    		u.setNumeroTelefono(nTelefono);
    		u.setPassword(password);
    		manager.persist(u);
    		manager.flush();
    		return true;
    	}    	
    }

}
